#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPort>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    statusRunLabel  =new QLabel(this);
    messageLabel = new QLabel(this);
    port = new QSerialPort(this);
     ui->statusbar->addPermanentWidget(messageLabel,1);


    setStatusIcon(false);
    cameraAdjustWidget = new QWidget(this);
    myCameraAdjust = new cameraAdjust(cameraAdjustWidget,port);

    sysOptionWidget = new QWidget(this);
    mySysOption = new sysOption(sysOptionWidget,port);

    myCameraAdjust->show();
    mySysOption->hide();

    myWait = new waitWidget(this,10,15,2);
    myWait->hide();

    waitTimer = new  QTimer(this);

    connect(mySysOption,SIGNAL(setPortStatus(bool)),this,SLOT(setStatusIcon(bool)));
    connect(mySysOption,SIGNAL(setStatusMessage(QString)),this,SLOT(setStatusMessageText(QString)));
    connect(mySysOption,SIGNAL(updateSysData()),myCameraAdjust,SLOT(updateSysData()));
    connect(waitTimer,SIGNAL(timeout()),this,SLOT(waitTimerOut()));
    connect(myCameraAdjust,SIGNAL(WaitMs(int)),this,SLOT(showWaitMS(int)));
    connect(mySysOption,SIGNAL(WaitMs(int)),this,SLOT(showWaitMS(int)));
    connect(ui->pushButton_3,SIGNAL(clicked()),mySysOption,SLOT(montorStop()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setStatusIcon(bool status)
{
    if(status == false){
        QImage Image(":/png/run_stop.png");
        QPixmap px = QPixmap::fromImage(Image);
        px = px.scaled(24,24,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        statusRunLabel->setPixmap(px);
    }else{
        QImage Image(":/png/run_start.png");
        QPixmap px = QPixmap::fromImage(Image);
        px = px.scaled(24,24,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        statusRunLabel->setPixmap(px);
    }
    ui->statusbar->addPermanentWidget(statusRunLabel,0);
}

void MainWindow::setStatusMessageText(QString text)
{
    messageLabel->setText(text);
}


void MainWindow::on_pushButton_2_clicked()
{
    if(cameraAdjustWidget->isVisible() == true){
        cameraAdjustWidget->hide();
        sysOptionWidget->show();
    }
}

void MainWindow::on_pushButton_clicked()
{
    if(cameraAdjustWidget->isVisible() == false){
        cameraAdjustWidget->show();
        sysOptionWidget->hide();
    }
}

void MainWindow::waitTimerOut()
{
    waitTimer->stop();
    myWait->hide();
}

void MainWindow::showWaitMS(int ms)
{
    waitTimer->start(ms);
    myWait->show();
}
